package com.wwz.leetcode;

import java.util.ArrayList;
import java.util.List;

/**
 * 给你二叉树的根节点 root ，返回它节点值的 前序 遍历。
 */
public class LeetCode144 {

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    public List<Integer> preorderTraversal(TreeNode root) {
        List<Integer> resList = new ArrayList<>();
        // 根左右
        if(root == null)
            return resList;
        // 递归
        recursion(root, resList);
        return resList;
    }

    private void recursion(TreeNode root, List<Integer> resList) {
        resList.add(root.val);
        if (root.left == null && root.right == null) {
            return;
        }
        if (root.left != null) {
            recursion(root.left, resList);
        }
        if (root.right != null) {
            recursion(root.right, resList);
        }
    }


}
